package com.likeycy.my.sort.cmp;

/**
 * @ClassName: BubbleSort1
 * @Description: 冒泡排序第二种写法
 * @Author: sodagreen
 * @Date: 2021/3/27 6:56
 * @Version: 1.0
 */
public class BubbleSort1<T extends Comparable<T>> extends Sort<T> {

    /**
     * 冒泡排序优化后第一版,前提是数组前提有序
     */
    @Override
    protected void sort() {
        for (int end = array.length - 1; end > 0; end--) {
            boolean sorted = true;
            for (int begin = 1; begin <= end; begin++) {
                if (cmp(begin, begin - 1) < 0) {
                    swap(begin, begin-1);
                    sorted = false;
                }
            }
            if (sorted) {
                break;
            }
        }
    }
}
